/**
 * 2024/01/01 - 2024/12/31
 **/
package com.snail.starter.utils.queue;

import com.snail.starter.utils.queue.consumer.QueueConsumer;
import com.snail.starter.utils.queue.producer.QueueProducer;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;

/**
 *
 * @author: (zg)
 * @Date: 2024/6/3 11:11
 * @Version: 1.0.0
 */
@Slf4j
public class QueueHolder {

    private final ArrayBlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(500);

    private final AtomicInteger atomicInteger = new AtomicInteger(0);

    public void start(){
        QueueConsumer<String> consumer = new QueueConsumer<>(blockingQueue, (s) -> {
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            log.info("consumer =====> " + s);
        });

        QueueProducer<String> producer = new QueueProducer<>(blockingQueue, () -> {

            List<String> list = new ArrayList<>();

            Stream.iterate(0, i -> i + 1).limit(10).forEach(i -> {

                list.add(String.valueOf(atomicInteger.incrementAndGet()));
            });
            return list;
        });

        consumer.start();

        producer.start();
    }

    public static void main(String[] args) {
        QueueHolder queueHolder = new QueueHolder();
        queueHolder.start();
    }

}
